package com.utils.httphelper;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Date;

import com.alibaba.fastjson.JSON;
//@Component("queryBigDataScoreJob")
public class QueryBigDataScoreJob {
	URL url = null;

	public void dealJob() throws Exception {
		// 筛选需要和风控交互的数据
		String querycode = "querycode";
		String ras="";
		String josnStr= JSON.toJSONString(ras);
//		String resMessage = JSON.parseObject(josnStr,String.class);
		// 和人人征信系统交互
		if (!"".equals(josnStr) && null != josnStr) {
			System.out.print("开始和大数据风控系统得到分数交互时间+" + new Date());
			utilsForBigdataFK(querycode, josnStr);
		} else {
			System.out.print("没有待打分数据！");
		}
	}

	/**
	 * <p>
	 * 和人人信征信系统交互 打分
	 * 
	 * @param applycode 申请号
	 * @param josnStr 拼装好的json串
	 */
	public void utilsForBigdataFK(String querycode, String josnStr) {
		// 异步请求默认地址 
		 String QUERY_RR_SEARCHs = "BigDataFk_score_address";
		try {
			long start = System.currentTimeMillis();
			String resMessage = connectionRR(josnStr,QUERY_RR_SEARCHs);
			long end = System.currentTimeMillis();
			System.out.print(resMessage+"与大数据风控系统交互并返回得到分数所用的时间=" + (end - start));
			// 处理返回数据
		} catch (Exception e) {
			// 抛出异常记录日志
			System.out.print("调用大数据风控系统打分失败：失败原因【" + e.getMessage() + "】交互地址为：【"
					+ url + "】");
		}

	}

	/**
	 * <p>
	 * 和交互开始打分
	 * 
	 * @param josnStr
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws ProtocolException
	 * @throws UnsupportedEncodingException
	 */
	private String connectionRR(String jsonStr, String url)
			throws Exception {
		HttpURLConnection con = null;
		BufferedReader in = null;
		String resultJsonStr = "";
		try {
			URL httpUrl = new URL(url);
			con = (HttpURLConnection) httpUrl.openConnection();
			con.setRequestMethod("POST");
			con.setRequestProperty("Content-Type",
					"application/json;charset=GBK");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setConnectTimeout(30000);
			con.connect();
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
					con.getOutputStream(), "UTF-8");
			outputStreamWriter.write(jsonStr);
			System.out.print("传递给系统的查询码数据=" + jsonStr);
			outputStreamWriter.flush();
			outputStreamWriter.close();
			
			//获取servlet返回值
			int responseCode = con.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				in = new BufferedReader(new InputStreamReader(
						con.getInputStream(), "UTF-8"));
				String line;
				while ((line = in.readLine()) != null) {
					resultJsonStr += line;
				}
				System.out.print("异步访问接口成功！");
			}
		} catch (Exception e) {
			System.out.print("异步访问网络接口失败！：" + e);
			e.printStackTrace();
	
		} finally {
			if (con != null) {
				con.disconnect();
			}
			if (in != null) {
				in.close();
			}
		}
		// 获取返回数据
		System.out.print("从调用的系统返回的分数结果为:" + resultJsonStr);
		return resultJsonStr;

	}
	
}
